layui.use('layer', function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer;
    layui.use('element', function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    });

    // 设置弹窗登录框
    layui.use('layer', function () { //独立版的layer无需执行这一句
        var $ = layui.jquery,
            layer = layui.layer; //独立版的layer无需执行这一句
    });
    //表单
    layui.use('form', function () {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    //日记点赞
    $(".diary-status").find("i").click(function (e) {
        e.preventDefault();
        var did = $(this).parent().attr("value");
        if ($(this).hasClass("layui-icon-heart")) {
            $(this).removeClass("layui-icon-heart");
            $(this).addClass("layui-icon-heart-fill");
            $(this).css("color", "hotpink");
            $(this).animate({
                fontSize: "24px"
            }, 300);
            $(this).animate({
                fontSize: "16px"
            }, 250);
            $.ajax({
                url: '/Diary/setzan/1/' + did,
                success(data) {
                    console.log("点赞成功!");
                }
            })
        } else {
            $(this).removeClass("layui-icon-heart-fill");
            $(this).addClass("layui-icon-heart");
            $(this).css("color", "#00F7DE");
            $(this).animate({
                fontSize: "24px"
            }, 300);
            $(this).animate({
                fontSize: "16px"
            }, 250);
            $.ajax({
                url: '/Diary/setzan/0/' + did,
                success(data) {
                    console.log("取消赞!");
                }
            })
        }
    });
    //日记点赞
    $(".diary-head").find("i").click(function () {
        if ($(this).hasClass("layui-icon-heart")) {
            $(this).removeClass("layui-icon-heart");
            $(this).addClass("layui-icon-heart-fill");
            $(this).css("color", "hotpink");
            $(this).animate({
                fontSize: "24px"
            }, 300);
            $(this).animate({
                fontSize: "20px"
            }, 250);
            $.ajax({
                url: '/Diary/setzan/1/' + $(".diary-head").attr("value"),
                success(data) {
                    console.log("点赞成功");
                }
            })
        } else {
            $(this).removeClass("layui-icon-heart-fill");
            $(this).addClass("layui-icon-heart");
            $(this).css("color", "#8470FF");
            $(this).animate({
                fontSize: "24px"
            }, 300);
            $(this).animate({
                fontSize: "20px"
            }, 250);
            $.ajax({
                url: '/Diary/setzan/0/' + $(".diary-head").attr("value"),
                success(data) {
                    console.log("取消赞");
                }
            })
        }
    });
    // 日记上一篇/下一篇
    $(".change-diary").find("span").click(function () {
        //端值加clss
        //端值提示
        if ($(this).text() == "上一篇") {
            if ($(this).hasClass("no-more")) {
                layer.msg('已经是第一篇了');
            } else {
                $.ajax({})
            }
        } else {
            if ($(this).hasClass("no-more")) {
                layer.msg('已经是最后一篇了');
            }
        }
    });

    //分页
    var count = $("#count").attr("value");

    function change(limit, page) {
        $.get("/Diary/diarylist",
            {
                limit: limit,
                page: page
            },
            function (data) {
                //加载后台返回的List集合数据
                // $(".main-list ul").append();
                for (var o in data.diaries) {
                    $("#diary-demo").append(
                        "<li>" +
                        "<a href='/Diary/diarydetail/n/" + data.diaries[o]["did"] + "'>" +
                        "<h2>" + (data.users[o]["name"] ? data.users[o]["name"] : data.users[o]["uid"]) + "</h2>" +
                        "<p>" + data.diaries[o]["content"] + "</p>" +
                        "<div class='diary-status' value=" + data.users[o]["uid"] + ">" +
                        "<i class='layui-icon layui-icon-heart'></i>" +
                        "<span id='time'>" + data.users[o]["date"] + "</span>" +
                        "</div>" +
                        "</a></li>"
                    );

                }
            },
            "json"
        );
    }

    layui.use('laypage', function () {
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
            elem: 'diary-list'
            , count: count
            , limit: 12
            , jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    $("#diary-demo").empty();
                    change(obj.limit, obj.curr);
                }
            }

        });
    });
    //发布日记
    $("#but").click(function () {
        var content = $(".diary-index-button textarea").val();
        var op = $(".diary-pulish option:selected").val();
        var type = $(".diary-content").attr("value");
        var url;
        var data;
        if (type == "y") {
            url = '/Diary/updatediary';
            var did = $(".diary-index-button").attr("value");
            data = {content: content, op: op, did: did};
        } else {
            url = '/Diary/diaryrelease';
            data = {content: content, op: op};
        }
        $.ajax({
            url: url,
            data: data,
            success: function (data) {
                layer.msg(data.msg);
                setTimeout(function () {
                    window.location.href = "/Diary/diaryedit";
                }, 1000)
            }
        });
    });
    //日记字数限制
    $(".diary-index-button").find("textarea").bind("input propertychange", function () {
        var text = $(this).val();
        var num = $(this).val().length;
        $(".diary-pulish").find("#font-num").html(num + "/255");
        if (num > 255) {
            layer.msg('超出字数限制了！');
            var lenText = text.substring(0, 255);
            $(this).val(lenText);
            $(".diary-pulish").find("#font-num").html(num - 1 + "/255");
        }
    });
    //公开-私密展示
    opened = function (obj) {
        $.ajax({
            url: '/Diary/opened/' + $(obj).attr("value"),
            success(data) {
                $(".diary-index-list ul").hide("slow");
                setTimeout(function () {
                    $(".diary-index-list ul").empty();
                    for (var o in data.diaries) {
                        $(".diary-index-list>ul").append(
                            "<li value=" + data.diaries[o]["did"] + ">" +
                            "<div class='diary-list-content'>" +
                            "<span>&nbsp;&nbsp;&nbsp;</span>" +
                            "<span>" + data.diaries[o]["content"] + "</span>" +
                            "</div>" +
                            "<div class='diary-list-status'>" +
                            "<span id='time'>" + data.diaries[o]["date"] + "</span>" +
                            "<span id='ledit'>编辑" +
                            "<i class='layui-icon'></i></span>" +
                            "<ul class='management-list layui-anim layui-anim-upbit'  value=" + data.diaries[o]["did"] + ">" +
                            "<li onclick='click1(this);'>删除</li>" +
                            "<li onclick='click2(this);'>修改</li>" +
                            "<li onclick='click3(this);'>" + data.op + "</li>" +
                            "</ul>" +
                            "</div>" +
                            "</li>"
                        );
                    }
                    setmenu();
                }, 800);
                $(".diary-index-list ul").show("slow");
            }
        })
    };
    click1 = function (obj) {
        var did = $(obj).parent().attr("value");
        console.log(did);
        $.ajax({
            url: '/Diary/deldiary/' + did,
            success(data) {
                layer.msg(data.msg);
                $(obj).parent().parent().parent().hide("slow");
                $(".diary-class-head .diary-class-span").find("a").eq(0).text(data.num1);
                $(".diary-class-head .diary-class-span").find("a").eq(1).text(data.num2);
            }
        })
    };
    click2 = function (obj) {
        var did = $(obj).parent().attr("value");
        console.log(did);
        window.location.href = '/Diary/diarydetail/y/' + did;
    };
    click3 = function (obj) {
        var did = $(obj).parent().attr("value");
        var op = $(obj).text();
        $.ajax({
            url: '/Diary/changeop',
            data: {did: did, op: op},
            success(data) {
                layer.msg(data.msg);
                $(obj).parent().parent().parent().hide("slow");
            }
        })
    };
    //箴言
    $(".diary-bless").stop().click(function () {
        soup();
    });
    //原料
    var arr1 = new Array(["岁月静好", "难得糊涂"], ["风平浪静", "海阔天空"], ["一川风月", "水明山秀"], ["月光倾城", "流绪微梦"], ["残花败柳", "夏花依旧"], [
        "悲欢自饮", "稚气未脱"
    ], ["半世晨晓", "一抹红尘"], ["流年花开", "浮生若梦"], ["折现浪漫", "梦回旧景"], ["夕夏温存", "相濡以沫"]);

    function soup() {
        //x上限，y下限
        var x = 9;
        var y = 0;
        var rand1 = parseInt(Math.random() * (x - y + 1) + y);
        var time = 2000;
        $(".diary-bless").find("#bless-left").fadeOut(time / 2);
        $(".diary-bless").find("#bless-right").fadeOut(time / 2);
        $(".diary-bless").find("#bless-left").fadeIn(time);
        $(".diary-bless").find("#bless-right").fadeIn(time);
        setTimeout(function () {
            $(".diary-bless").find("#bless-left").html(arr1[rand1][0]);
            $(".diary-bless").find("#bless-right").html(arr1[rand1][1]);
        }, 1000);
        return rand1;
    }

    //2分钟自动执行一次
    setInterval(soup, 20000);

    // 单个日记列表菜单
    setmenu();

    function setmenu() {
        $(".diary-list-status").find("#ledit").on("click", function (e) {
            e.stopPropagation();
            if ($(this).siblings(".management-list").is(":hidden")) {
                $(this).siblings(".management-list").show();
            } else {
                $(this).siblings(".management-list").hide();
            }
            $(document).click(function () {
                if ($(".diary-list-status").find(".management-list").is(":visible")) {
                    $(".diary-list-status").find(".management-list").hide();
                }
            });
        });
    }
});
